package com.creatshare.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.creatshare.bean.User;
import com.creatshare.util.MD5StringUtils;
import com.creatshare.util.ResultInfo;
import com.creatshare.util.ValidateUtils;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public @ResponseBody Object login(User user) throws Exception {
		boolean flag = ValidateUtils.vaildate(user.getUsername(), user.getPassword());
		if (!flag) {
			resultInfo.setCode(-1);
			resultInfo.setData(ResultInfo.USERNAME_AND_PASSWORD_EMPTY);
			return JSONObject.toJSON(resultInfo);
		}
		user = userService.getUserByUserNameAndPassword(user.getUsername(),
				MD5StringUtils.MD5Encode(user.getPassword()));
		if (user == null) {
			resultInfo.setCode(-2);
			resultInfo.setData(ResultInfo.USERNAME_AND_PASSWORD_FAULT);
		} else {
			resultInfo.setCode(1);
			resultInfo.setData(user);
			session.setAttribute("user", user);
		}
		return JSONObject.toJSON(resultInfo);
	}

/*	@RequestMapping(value = "/", method = RequestMethod.POST)
	public @ResponseBody Object insert(User user) {
		user.setPassword(MD5StringUtils.MD5Encode(user.getPassword()));
		if (userService.insert(user) != 0) {
			resultInfo.setCode(1);
			resultInfo.setData(ResultInfo.USER_INSERT_INTO_SUCCESS);
		} else {
			resultInfo.setCode(-1);
			resultInfo.setData(ResultInfo.USER_INSERT_INTO_ERROR);
		}
		System.out.println(user.getUserid());
		return JSONObject.toJSON(resultInfo);
	}*/
}
